<?php

if (! defined('IN_ACHAO_ORG'))
{
    trigger_error('Hack attempting.', E_USER_ERROR);
}


class Manager
{
    var $err = null;
    var $_pagesize = 20;
    var $cache;
    var $proxy    ='';

    function __construct()
    {
        $this->Manager();
    }

    /**
     * Constructor
     *
     * @return object
     */
    function Manager()
    {
    	$this->proxy = new Proxy();
        //TODO: insert your code
    }
    /**
     * Get query params
     * @author  redstone
     * @param   int     $page
     * @param   array   $condition
     * @param   string  $default_sort
     * @param   int     $pagesize
     *
     * @return array
     */
    function query_params($page, $condition, $default_sort, $pagesize = 0)
    {

        if ($pagesize > 0) $this->set_pagesize($pagesize);
        $arr['number']  = $this->_get_pagesize();
        $arr['start']   = ($page - 1) * $arr['number'];
        $arr['count']   = $this->get_count($condition);
        $arr['where']   = $this->_make_condition($condition);
        $arr['info']    = $this->_page_info($page, $arr['count']);
        $arr['sort']    = (!empty($_GET['sort']) && preg_match('/^[\w]+$/', $_GET['sort']) > 0) ? trim($_GET['sort']) : $default_sort;
        $arr['order']   = (!empty($_GET['order']) && strtolower($_GET['order']) == 'asc') ? 'ASC' : 'DESC';
        ($arr['sort'] == 'goods_name') && $arr['sort'] = 'eg.goods_name';
        return $arr;
    }

    /**
     *
     * @param   array   $condition
     *
     * @return  int
     */
    function get_count($condition)
    {
        return 0;
    }

    function _page_info($page, $rec_count)
    {
        $segment    = 4;
        $pagesize   = $this->_get_pagesize();
        $page_count = ceil((int)$rec_count / $pagesize);

        if ($page_count < 1)
        {
            $page_count = 1;
        }
        $prev_page  = ($page > 1) ? $page - 1 : 1;
        $next_page  = ($page < $page_count) ? $page + 1 : $page_count;
        $page  =($page>$page_count) ? $page_count : $page;
        $result     = array('rec_count'     => $rec_count,
                            'page_count'    => $page_count,
                            'prev_page'     => $prev_page,
                            'next_page'     => $next_page,
                            'curr_page'     => $page);
        return $result;
    }
    /**
     *
     * @return  int
     */
    function _get_pagesize()
    {
        if ($this->_pagesize <= 0) $this->_pagesize = 20;

        return $this->_pagesize;
    }

    function set_pagesize($pagesize = 0)
    {
        $this->_pagesize = $pagesize;
    }

    /**
     *
     * @author  scottye
     * @param   array   $condition
     * @return  string
     */
    function _make_condition($condition)
    {
        $where  = '1';

        if (!empty($condition['store_is_open']))
        {
            $where .= " AND store_id " . db_create_in($this->get_open_store());
        }


        return $where;

    }

    /**
     * get page number for current request
     *
     * @return  int
     */
    function get_page()
    {
        $page = 0;
        if (isset($_GET['page']))
        {
            $page = intval($_GET['page']);
        }

        if ($page <= 0)
        {
            $page = 1;
        }

        return $page;
    }

    function getCache()
    {
        if($this->cache == null)
        {
            $this->cache = UE_Cache::getInstance(DB_CACHE_TYPE);
        }
        return $this->cache;
    }
   
     /**
     * get userId  for current request
     *
     * @return  int
     */
    function getUserId($uid=0)
    {
    	return  $uid ? $uid : ($_SESSION['user_id'] ? $_SESSION['user_id']:0);
    }
    /**
     * getOrderDirec
     *
     * @return  string
     */
    function getOrderDirec($orderDirec)
    {
    	return   in_array(strtolower($orderDirec),array('asc','desc'))? strtolower($orderDirec):'desc';
    }
    
    /**
     * getReqDate
     * 格式 ：yyyy-MM-dd HH24:mi:ss
     * @return  string
     */
    function getReqDate()
    {
    	return   date('Y-m-d H:i:s');
    }
   
};

?>
